﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

using Intergr8.Data;

namespace Intergr8.Data.SQLDataSource
{
    public class SQLDataProvider : SecuredDataProvider
    {

        /// <summary>
        /// Default value for Command
        /// </summary>

        private string _command = "";
        private string _connection = "";
        #region Initialise
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            try
            {
                System.Diagnostics.Trace.WriteLine("Initialising SQLDataProvider");

                // Initialise the base first
                base.Initialize(name, config);

                // Pull out any of our custom attributes
                if (config["Command"] != null)
                {
                    _command = config["Command"];
                }
                else
                {
                    throw new DataProviderException("SQLDataProvider", "The SQLDataProvider needs a 'Command' property to execute against the database connection.");
                }

                if (config["ConnectionString"] != null)
                {
                    _connection = config["ConnectionString"];
                }
                else
                {
                    throw new DataProviderException("SQLDataProvider","The SQLDataProvider needs a 'ConnectionString' property to complete the database connection.");
                }

                if (_connection == "")
                {
                    throw new DataProviderException("SQLDataProvider","The SQLDataProvider needs a 'ConnectionString' value to complete the database connection.");
                }
                if (_command == "")
                {
                    throw new DataProviderException("SQLDataProvider", "The SQLDataProvider needs a 'Command' value to complete the database connection.");
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("SQLDataProvider Exception " + ex.Message);
                throw ex;
            }
            finally{
            }
        }
        #endregion

        public override System.Data.DataTable Fetch()
        {
            SqlConnection conn = null;
            try
            {
                System.Diagnostics.Debug.WriteLine("SQLDataProvider.Fetch");

                SetContext();

                System.Data.DataTable dt = new System.Data.DataTable();

                // Connect to SQL Server
                conn = new SqlConnection(_connection);

                SqlCommand comm = new SqlCommand(_command, conn);

                conn.Open();

                // Query data
                SqlDataAdapter da = new SqlDataAdapter(comm);
                da.Fill(dt);

                // Return data table
                return dt;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError(ex.Message);
                throw new DataProviderException("SQLDataProvider", "Error generating a DataTable", ex);
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                ResetContext();
            }

        }


    }
}
